package cn.com.fuhuitong.base

import android.app.Activity
import com.blankj.utilcode.util.LogUtils
import java.util.*

object ActivityCollector {
    var activities: MutableList<Activity> = ArrayList()
    fun addActivity(activity: Activity) {
        activities.add(activity)
    }

    fun removeActivity(activity: Activity?) {
        activities.remove(activity)
    }

    fun finishActivity(activity: Activity?) {
        var activity = activity
        if (activity != null) {
            activities.remove(activity)
            activity.finish()
        }
    }

    /**
     * 结束指定类名的Activity
     *
     * @param cls
     */
    fun finishActivity(cls: Class<*>?) {
        for (i in 0 until activities.size) {
            if (activities[i] == cls) {
                finishActivity(activities[i])
            }
        }
    }

    fun finishAll() {
        for (activity in activities) {
            if (!activity.isFinishing) {
                activity.finish()
            }
        }
    }
}